/*
 Copyright 2009 Abdulla Abdurakhmanov (abdulla@latestbit.com)
 Original sources are available at www.latestbit.com

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.

 StdRoundRobinCoordinator.hpp. Created on: 29.09.2009 14:36:24 
 */

#ifndef STDROUNDROBINCOORDINATOR_HPP_
#define STDROUNDROBINCOORDINATOR_HPP_

#include "smartobjects/SmartObjectsDefs.hpp"
#include "StdFailOverCoordinator.hpp"
#include "ICoordinatorContext.hpp"

namespace smartobjects {

class StdRoundRobinCoordinator : public StdFailOverCoordinator {
public:
	SO_DLLPRFX static const std::string COORDINATOR_NAME;

public:
	SO_DLLPRFX StdRoundRobinCoordinator();
	SO_DLLPRFX virtual ~StdRoundRobinCoordinator();
	SO_DLLPRFX virtual const std::string& getName() ;
	SO_DLLPRFX virtual ConnectionHandler* getConnectionHandlerToSend ( IManagedConnection* ) ;

protected:
	class Context : public ICoordinatorContext {
	public:
		inline Context(): m_nRoundRobinCounter(0) {}
		SO_DLLPRFX inline virtual ~Context() {}
		SO_DLLPRFX inline virtual void reset() { m_nRoundRobinCounter = 0; }

		SO_DLLPRFX inline int updateRoundRobinCounter(unsigned int nMaxCounterValue) {
			if(m_nRoundRobinCounter >= nMaxCounterValue) {
				m_nRoundRobinCounter = 0;
			}
			return m_nRoundRobinCounter++;
		}
	protected:
		unsigned int m_nRoundRobinCounter;
	};

protected:
	SO_DLLPRFX virtual void insertHandler(IManagedConnection*, ConnectionHandler*);

};

}

#endif /* STDROUNDROBINCOORDINATOR_HPP_ */
